package org.ceteca.explica.client.management;

import java.util.ArrayList;
import java.util.HashSet;

import org.ceteca.explica.client.gui.ExplicaDateSelect;
import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.RegistryOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.management.DocumentLineOT;
import org.ceteca.explica.core.management.InterfaceManagement;
import org.ceteca.explica.core.management.MovementOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.PartnerOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.warehouse.InterfaceWarehouse;
import org.ceteca.explica.core.warehouse.LotOT;
import org.ceteca.explica.core.warehouse.ProductOT;
import org.ceteca.explica.core.warehouse.WarehouseOT;
import org.ceteca.explica.server.management.bl.ManagementBLL;
import org.ceteca.javaglade.InterfaceGladeGtk;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;

public class MovementClient extends CommonClient {

	
	
	private Text w_version, w_id, w_date, w_document_line, w_product, w_warehouse, w_partner;
	private Text w_lot, w_quantity, w_costPrice, w_salesPrice, w_cost, w_total;
	private Button w_select_date, w_select_document_line, w_select_product, w_select_warehouse, w_select_partner;
	private Button w_select_lot, w_buttonIn, w_buttonOut;
	private ExplicaDateSelect selectDate;
	
	public MovementClient() {
		super(MovementOT.class,
				InterfaceManagement.EXPLICA_EDIT_MOVEMENT_GLADE_PATH,
				InterfaceManagement.EXPLICA_EDIT_MOVEMENT_DIALOG_NAME);
		
	}
	
	@Override
	public void clearEntityData() {
		MovementOT pll = new MovementOT();
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity().getEntity().getKey()
					.equals(InterfaceManagement.EXPLICA_ENTITY_DOCUMENT_LINE_ID)) {
				DocumentLineOT parentDocumentLine = (DocumentLineOT) entityDetailView
						.getSubentityData().getParentEntity()
						.getSelectedEntity();
				pll.setDocumentLine(parentDocumentLine);
				pll.setProduct(parentDocumentLine.getProduct());
				pll.setWarehouse(parentDocumentLine.getWarehouse());
				if (parentDocumentLine.getDocument()!=null){
					pll.setDate(parentDocumentLine.getDocument().getDateDocument());
					pll.setPartner(parentDocumentLine.getDocument().getPartner());
					
				}
				w_document_line.setEnabled(false);
				w_select_document_line.setEnabled(false);
				w_partner.setEnabled(false);
				w_select_partner.setEnabled(false);
				w_date.setEnabled(false);
				w_select_date.setEnabled(false);
				w_product.setEnabled(false);
				w_select_product.setEnabled(false);
				w_warehouse.setEnabled(false);
				w_select_warehouse.setEnabled(false);
				
			}

		}

		setWidgetsValues(pll);
	}


	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfaceManagement.EXPLICA_ENTITY_DOCUMENT_LINE_ID,
				InterfaceManagement.EDIT_MOVEMENT_DOCUMENTLINE_WIDGETNAME,
				this, w_select_document_line));
		lSelectionSubentities
				.add(new SelectionSubentityOT(
						InterfaceWarehouse.EXPLICA_ENTITY_PRODUCT_ID,
						InterfaceManagement.EDIT_MOVEMENT_PRODUCT_WIDGETNAME,
						this, w_select_product));
		lSelectionSubentities
		.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_PARTNER_ID,
				InterfaceManagement.EDIT_MOVEMENT_PARTNER_WIDGETNAME,
				this, w_select_partner));
		lSelectionSubentities
		.add(new SelectionSubentityOT(
				InterfaceWarehouse.EXPLICA_ENTITY_WAREHOUSE_ID,
				InterfaceManagement.EDIT_MOVEMENT_WAREHOUSE_WIDGETNAME,
				this, w_select_warehouse));
		lSelectionSubentities
		.add(new SelectionSubentityOT(
				InterfaceWarehouse.EXPLICA_ENTITY_LOT_ID,
				InterfaceManagement.EDIT_MOVEMENT_LOT_WIDGETNAME,
				this, w_select_lot));

	}

	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub

	}

	@Override
	public BaseVO getEntityData() {
		MovementOT ent=new MovementOT();
		if (this.entEditView != null) {
			ent.setCostPrice(Converter.getInstance().parseBigDecimal(
					w_costPrice.getText()));
			ent.setDate(Converter.getInstance().parseDate(
					w_date.getText()));
			ent.setQuantity(Converter.getInstance().parseBigDecimal(w_quantity.getText()));
			ent.setSalesPrice(Converter.getInstance().parseBigDecimal(w_salesPrice.getText()));
			ent.setDocumentLine((DocumentLineOT)w_document_line.getData());
			ent.setLot((LotOT)w_lot.getData());
			ent.setPartner((PartnerOT)w_partner.getData());
			ent.setProduct((ProductOT)w_product.getData());
			ent.setWarehouse((WarehouseOT)w_warehouse.getData());

			if (w_buttonIn.isEnabled())
				ent.setIncrementStock(true);
			else
				ent.setIncrementStock(false);
			
			
			
			
			if (selectedEntity != null) {
				ent.setVersion(((MovementOT) selectedEntity).getVersion());
				ent.setIdMovement(((MovementOT) selectedEntity)
						.getIdMovement());
			}

		}
		return ent;
	}

	@Override
	public void getWidgets() {
		
		
		ArrayList<String> codErrors=new ArrayList<String>();
		
		String nWidget=InterfaceManagement.EDIT_MOVEMENT_ID_WIDGETNAME;
		if ((w_id = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_COST_WIDGETNAME;
		if ((w_cost = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_COSTPRICE_WIDGETNAME;
		if ((w_costPrice = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_DATE_WIDGETNAME;
		if ((w_date = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_DOCUMENTLINE_WIDGETNAME;
		if ((w_document_line = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_LOT_WIDGETNAME;
		if ((w_lot = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_PARTNER_WIDGETNAME;
		if ((w_partner = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_PRODUCT_WIDGETNAME;
		if ((w_product = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_QUANTITY_WIDGETNAME;
		if ((w_quantity = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_SALESPRICE_WIDGETNAME;
		if ((w_salesPrice = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_TOTAL_WIDGETNAME;
		if ((w_total = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_VERSION_WIDGETNAME;
		if ((w_version = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		nWidget=InterfaceManagement.EDIT_MOVEMENT_WAREHOUSE_WIDGETNAME;
		if ((w_warehouse = ((Text) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}

		nWidget=InterfaceManagement.EDIT_MOVEMENT_SELECT_DATE_WIDGETNAME;
		if ((w_select_date = ((Button) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		
		nWidget=InterfaceManagement.EDIT_MOVEMENT_SELECT_DOCUMENT_LINE_WIDGETNAME;
		if ((w_select_document_line = ((Button) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		
		nWidget=InterfaceManagement.EDIT_MOVEMENT_SELECT_LOT_WIDGETNAME;
		if ((w_select_lot = ((Button) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		
		nWidget=InterfaceManagement.EDIT_MOVEMENT_SELECT_PARTNER_WIDGETNAME;
		if ((w_select_partner = ((Button) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		
		nWidget=InterfaceManagement.EDIT_MOVEMENT_SELECT_PRODUCT_WIDGETNAME;
		if ((w_select_product = ((Button) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		
		nWidget=InterfaceManagement.EDIT_MOVEMENT_SELECT_WAREHOUSE_WIDGETNAME;
		if ((w_select_warehouse = ((Button) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		
		nWidget=InterfaceManagement.EDIT_MOVEMENT_BUTTON_IN_WIDGETNAME;
		if ((w_buttonIn = ((Button) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		
		nWidget=InterfaceManagement.EDIT_MOVEMENT_BUTTON_OUT_WIDGETNAME;
		if ((w_buttonOut = ((Button) this.getWidget(
				nWidget))) == null) {
			codErrors.add(nWidget);
		}
		
		
		
		
		if (!codErrors.isEmpty()){
			for (String error: codErrors){
				this.logger.debug("Error obteniendo widget: "+error);
				System.out.println("Error obteniendo widget: "+error);
			}
			fireExceptionAndExit("Error obteniendo widgets");
		}
	}

	@Override
	public void init_specific_components() {
		getWidgets();
		ExplicaGUIBuilder.getInstance().connectSignal(
				InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, w_select_date,
				"on_select_date_clicked", this);

		
		
	}

	@Override
	public void setEditionMode(boolean enabled) {
		w_version.setEnabled(false);
		w_id.setEnabled(false);
		w_document_line.setEnabled(false);
		w_select_document_line.setEnabled(false);
		w_cost.setEnabled(false);
		w_total.setEnabled(false);
		w_costPrice.setEnabled(enabled);
		


		w_partner.setEnabled(enabled);


		w_salesPrice.setEnabled(enabled);



		w_select_partner.setEnabled(enabled);


		
		if (w_id.getText().equals("0")|| w_id.getText().isEmpty()){
			w_buttonIn.setEnabled(enabled);
			w_buttonOut.setEnabled(enabled);
			w_date.setEnabled(enabled);
			w_select_date.setEnabled(enabled);
			w_lot.setEnabled(enabled);
			w_select_lot.setEnabled(enabled);
			w_product.setEnabled(enabled);
			w_select_product.setEnabled(enabled);
			w_quantity.setEnabled(enabled);
			w_select_warehouse.setEnabled(enabled);
			w_warehouse.setEnabled(enabled);
		}else{
			w_buttonIn.setEnabled(false);
			w_buttonOut.setEnabled(false);
			w_date.setEnabled(false);
			w_select_date.setEnabled(false);
			w_lot.setEnabled(false);
			w_select_lot.setEnabled(false);
			w_product.setEnabled(false);
			w_select_product.setEnabled(false);
			w_quantity.setEnabled(false);
			w_select_warehouse.setEnabled(false);
			w_warehouse.setEnabled(false);
		}

	}

	@Override
	public void setWidgetsValues(BaseVO entity) {
		MovementOT ent=(MovementOT)entity;
		w_version.setText(Converter.getInstance().toString(ent.getVersion()));
		w_id.setText(Converter.getInstance().toString(
				ent.getIdMovement()));
		w_costPrice.setText(Converter.getInstance().toString(ent.getCostPrice()));
		w_date.setText(Converter.getInstance().toString(ent.getDate()));
		w_quantity.setText(Converter.getInstance().toString(ent.getQuantity()));
		w_salesPrice.setText(Converter.getInstance().toString(ent.getSalesPrice()));
		if (ent.getIncrementStock()!=null){
		w_buttonIn.setEnabled(ent.getIncrementStock());
		w_buttonOut.setEnabled(!ent.getIncrementStock());
		}else{
			w_buttonIn.setEnabled(false);
			w_buttonOut.setEnabled(true);
		}
		if (ent.getQuantity()!=null){
			if (ent.getCostPrice()!=null)
				w_cost.setText(Converter.getInstance().toString(ent.getCostPrice().multiply(ent.getQuantity())));
			if (ent.getSalesPrice()!=null)
				w_total.setText(Converter.getInstance().toString(ent.getSalesPrice().multiply(ent.getQuantity())));
		}else{
			w_cost.setText("0");
			w_total.setText("0");
		}
		
		if (ent.getDocumentLine()!=null){
			w_document_line.setText(Converter.getInstance().toString(ent.getDocumentLine().getIdDocumentLine()));
			w_document_line.setData(ent.getDocumentLine());
		}
		
		if (ent.getPartner()!=null){
			w_partner.setText(Converter.getInstance().toString(ent.getPartner().getName()));
			w_partner.setData(ent.getPartner());
		}
		if (ent.getProduct()!=null){
			w_product.setText(Converter.getInstance().toString(ent.getProduct().getDescription()));
			w_product.setData(ent.getProduct());
		}
		if (ent.getWarehouse()!=null){
			w_warehouse.setText(Converter.getInstance().toString(ent.getWarehouse().getDescription()));
			w_warehouse.setData(ent.getWarehouse());
		}
		if (ent.getLot()!=null){
			w_lot.setText(Converter.getInstance().toString(ent.getLot().getDescription()));
			w_lot.setData(ent.getLot());
		}
		
		
		
	}

	public void on_select_date_clicked(Event e){
		selectDate = new ExplicaDateSelect(this.logger, this, "on_date_selected");
		selectDate.build();
		selectDate.show(Converter.getInstance().parseDate(
				w_date.getText()));

	}
	
	public void on_date_selected(Event e) {

		w_date.setText(Converter.getInstance().toString(selectDate.getDate()));

}
	
	
	
	@Override
	public void fillEntityData() {
		MovementOT ent = (MovementOT) this.getSelectedEntity();
		this.setWidgetsValues(ent);
	}
	
	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}

	

	
}
